const { AsyncParallelHook } = require('tapable')

let hook = new AsyncParallelHook(['name'])

// 对于异步钩子的使用，在添加事件监听时会存在三种方式： tap tapAsync tapPromise

/*
// 01 tap
console.time('time')
hook.tap('fn1', (name) => {
  console.log('fn1--->', name)
})

hook.tap('fn2', (name) => {
  console.log('fn2--->', name)
})

hook.callAsync('nick', () => {
  console.log('excute callback')
  console.timeEnd('time')
})
*/

/*
// 02 tapAsync
console.time('time')
hook.tapAsync('fn1', (name, callback) => {
  setTimeout(() => {
    console.log('fn1--->', name)
    callback()
  }, 1000)
})

hook.tapAsync('fn2', (name, callback) => {
  setTimeout(() => {
    console.log('fn2--->', name)
    callback()
  }, 2000)
})

hook.callAsync('lg', () => {
  console.log('excute callback')
  console.timeEnd('time')
})
*/

// 03 promise
console.time('time')
hook.tapPromise('fn1', (name) => {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      console.log('fn1--->', name)
      resolve()
    }, 1000)
  })
})

hook.tapPromise('fn2', (name) => {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      console.log('fn2--->', name)
      resolve()
    }, 2000)
  })
})

hook.promise('foo').then(() => {
  console.log('excute')
  console.timeEnd('time')
})
